<?php
/*
Plugin Name: WP-PageNavi
Plugin URI: http://lesterchan.net/portfolio/programming.php
Description: Adds a more advanced paging navigation to your WordPress blog.
Version: 2.20
Author: Lester 'GaMerZ' Chan
Author URI: http://lesterchan.net
*/


/*  
	Copyright 2007  Lester Chan  (email : gamerz84@hotmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


### Function: Page Navigation CSS
add_action('wp_head', 'pagenavi_css');
function pagenavi_css() {
	if (!is_single()){
$siteurl = get_option('siteurl');
?>
<!-- Start Of Script Generated By WP-PageNavi 2.20 -->
	<link rel="stylesheet" href="<?php echo $siteurl;?>/wp-content/plugins/pagenavi/pagenavi-css.css" type="text/css" media="screen" />
	<script type="text/javascript" src="<?php echo $siteurl;?>/wp-content/themes/ETiV/js/jquery.js"></script>
	<script type="text/javascript" src="<?php echo $siteurl;?>/wp-content/themes/ETiV/js/iutil.js"></script>
	<script type="text/javascript" src="<?php echo $siteurl;?>/wp-content/themes/ETiV/js/fisheye.js"></script>
	<script type="text/javascript">
		$(function(){
				$('#wp-pagenavi').Fisheye({maxWidth:30,items:'a',container:'.wp-navi',itemWidth:16,proximity:90,halign:'center'});
			}
		);
	</script>
<!-- End Of Script Generated By WP-PageNavi 2.20 -->

<?php
	}
}


### Function: Page Navigation: Boxed Style Paging
function wp_pagenavi() {
	global $wpdb, $wp_query;
	if (!is_single()) {
		$request = $wp_query->request;
		$posts_per_page = intval(get_query_var('posts_per_page'));
		$paged = intval(get_query_var('paged'));
		//$pagenavi_options = get_option('pagenavi_options');
		
	$pagenavi_options = array();
	$pagenavi_options['pages_text'] = '共%TOTAL_PAGES%页:';
	$pagenavi_options['current_text'] = '%PAGE_NUMBER%';
	$pagenavi_options['page_text'] = '%PAGE_NUMBER%';
	$pagenavi_options['first_text'] = '返回首页';
	$pagenavi_options['last_text'] = '跳到最后';
	$pagenavi_options['next_text'] = '&gt;';
	$pagenavi_options['prev_text'] = '&lt;';
	$pagenavi_options['dotright_text'] = '+';
	$pagenavi_options['dotleft_text'] = '+';
	$pagenavi_options['num_pages'] = 9;
	$pagenavi_options['always_show'] = 0;
		
		$numposts = $wp_query->found_posts;
		$max_page = $wp_query->max_num_pages;

		if(empty($paged) || $paged == 0) {
			$paged = 1;
		}
		$pages_to_show = intval($pagenavi_options['num_pages']);
		$pages_to_show_minus_1 = $pages_to_show-1;
		$half_page_start = floor($pages_to_show_minus_1/2);
		$half_page_end = ceil($pages_to_show_minus_1/2);
		$start_page = $paged - $half_page_start;
		if($start_page <= 0) {
			$start_page = 1;
		}
		$end_page = $paged + $half_page_end;
		if(($end_page - $start_page) != $pages_to_show_minus_1) {
			$end_page = $start_page + $pages_to_show_minus_1;
		}
		if($end_page > $max_page) {
			$start_page = $max_page - $pages_to_show_minus_1;
			$end_page = $max_page;
		}
		if($start_page <= 0) {
			$start_page = 1;
		}
		if($max_page > 1 || intval($pagenavi_options['always_show']) == 1) {
			$pages_text = str_replace("%CURRENT_PAGE%", $paged, $pagenavi_options['pages_text']);
			$pages_text = str_replace("%TOTAL_PAGES%", $max_page, $pages_text);
			echo '<div id="wp-pagenavi">'."\n";
					echo '<span class="pages">'.$pages_text.'</span>'."\n";
					echo '<div class="wp-navi">'."\n";
					if ($paged >= $pages_to_show_minus_1 && $pages_to_show < $max_page) {
						echo '<a href="'.get_pagenum_link().'" title="'.$pagenavi_options['first_text'].'">H<span>'.$pagenavi_options['first_text'].'</span></a>';
						if(!empty($pagenavi_options['dotleft_text'])) {
							echo '<a class="extend">'.$pagenavi_options['dotleft_text'].'</a>';
						}
					}
					previous_posts_link($pagenavi_options['prev_text']);
					for($i = $start_page; $i  <= $end_page; $i++) {						
						if($i == $paged) {
							$current_page_text = str_replace("%PAGE_NUMBER%", $i, $pagenavi_options['current_text']);
							echo '<a href="#" class="current" title="当前第 '.$current_page_text.' 页">C<span>当前第 '.$current_page_text.' 页</span></a>';
						} else {
							$page_text = str_replace("%PAGE_NUMBER%", $i, $pagenavi_options['page_text']);
							echo '<a href="'.get_pagenum_link($i).'" title="第 '.$page_text.' 页">*<span>第 '.$page_text.' 页</span></a>';
						}
					}
					next_posts_link($pagenavi_options['next_text'], $max_page);
					if ($end_page < $max_page) {
						if(!empty($pagenavi_options['dotright_text'])) {
							echo '<a class="extend">'.$pagenavi_options['dotright_text'].'</a>';
						}
						echo '<a href="'.get_pagenum_link($max_page).'" title="'.$pagenavi_options['last_text'].'">L<span>'.$pagenavi_options['last_text'].'</span></a>';
					}
			echo '</div>'."\n".'</div>'."\n";
		}
	}
}

?>